import Mock from 'mockjs'
import user from './user'

// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}

let List = []
const count = 200
for (let i = 1; i <= 200; i++) {
    List.push(
        Mock.mock({
            id: Mock.Random.guid(),
            'name|1': ['iPhone 12Pro', 'iPhone 11Pro', 'iPhone 12', '华为 P10', '华为 Mate9', '华为 荣耀8', '三星 Galaxy S', '三星 Galaxy Note', '三星 Galaxy Fold', '魅族 Pro7', '魅族 M8', '魅族 MX4 Pro', 'vivo X20', 'vivo X20 Plus', 'vivo X9', 'oppo R9s', 'oppo A77', 'oppo Reno6 Pro', 'Redmi K50', 'Redmi Note11', 'Redmi 10A', '诺基亚', '小灵通', '手机模型'],
            number: Mock.Random.natural(1, 6),
            'age|18-60': 1,
            money: Mock.Random.natural(2000, 12000),
            date: Mock.Random.date('2022-' + 'MM-dd hh:mm:ss'),
            'type|1': ['待付款', '待发货', '待收货', '待评价', '已评价'],
            shop: Mock.Random.guid(),
            'shop|1': ['数码旗舰店一店', '数码旗舰店二店', '数码旗舰店三店', '数码旗舰店四店', '数码旗舰店五店', '数码旗舰店六店', '数码旗舰店七店', '数码旗舰店八店'],
            addr: Mock.mock('@county(true)'),
        })
    )
}

export default {
    /**
     * 获取列表
     * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
     * @param name, page, limit
     * @return {{code: number, count: number, data: *[]}}
     */
    getMallList: config => {
        const { id, name, addr, type, page = 1, limit = 20, oldData, newData } = param2Obj(config.url)
        const mockList = List.filter(user => {
            if (user.id.indexOf(id) === -1 || user.name.indexOf(name) === -1 || user.addr.indexOf(addr) === -1 || user.type.indexOf(type) === -1 || (+oldData.substring(0, 4) > +user.date.substring(0, 4) || (+oldData.substring(0, 4) == +user.date.substring(0, 4) && (+oldData.substring(5, 7) > +user.date.substring(5, 7) || (+oldData.substring(5, 7) == +user.date.substring(5, 7) && +user.date.substring(8, 10) < +oldData.substring(8, 10))))) || ((+newData.substring(0, 4) < +user.date.substring(0, 4) || (+newData.substring(0, 4) == +user.date.substring(0, 4)) && +newData.substring(5, 7) < +user.date.substring(5, 7) || (+newData.substring(5, 7) == +user.date.substring(5, 7) && +user.date.substring(8, 10) > +newData.substring(8, 10)))) && (newData != '' && oldData != '')) return false
            return true
        })
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        return {
            code: 20000,
            count: mockList.length,
            list: pageList,
            user: user
        }
    },
    /**
     * 删除用户
     * @param id
     * @return {*}
     */
    deleteMall: config => {
        const { id } = JSON.parse(config.body)
        if (!id) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(u => u.id !== id)
            return {
                code: 20000,
                message: '删除成功'
            }
        }
    },
    /**
     * 批量删除
     * @param config
     * @return {{code: number, data: {message: string}}}
     */
    batchremove: config => {
        let { ids } = param2Obj(config.url)
        ids = ids.split(',')
        List = List.filter(u => !ids.includes(u.id))
        return {
            code: 20000,
            data: {
                message: '批量删除成功'
            }
        }
    },
}